import pygame
import sys
import traceback
import random
import myplane
import enemy
import bullet
import supply

pygame.init()
pygame.mixer.init()

BLACK=(0,0,0)
WHITE=(255,255,255)
RED=(255,0,0)
GREEN =(0,255,0)
size = width,height = 480,700
screen=pygame.display.set_mode(size)
######一定记得要写标题########
pygame.display.set_caption('飞机大战')
background=pygame.image.load('images/background.png').convert_alpha()
again = pygame.image.load('images/again.png').convert_alpha()
again_rect=again.get_rect()

gameover=pygame.image.load('images/gameover.png').convert_alpha()
gameover_rect = gameover.get_rect()

best_score= 666
# 载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.1)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.5)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)
def add_smallenemy(group1,group2,num):
    for i in range(num):
        e1=enemy.Smallenemy(size)
        group1.add(e1)
        group2.add(e1)
def add_midenemy(group1,group2,num):
    for i in range(num):
        e2=enemy.Midenemy(size)
        group1.add(e2)
        group2.add(e2)
def add_bigenemy(group1,group2,num):
    for i in range(num):
        e3=enemy.Bigenemy(size)
        group1.add(e3)
        group2.add(e3)
def inc_speed(group1,num):
    for each in group1:
        each.speed+=num

def main():
    #各种初始化
    pygame.mixer.music.play(-1)
    clock= pygame.time.Clock()
    
    #生成我方飞机
    me=myplane.Myplane(size)

    #生成普通子弹
    bullets = []
    bullet1 = []
    bullet1_index=0
    BULLET1_NUM = 4
    for i in range(BULLET1_NUM ):
        bullet1.append(bullet.Bullet1(me.rect.midtop))
        
    #生成超级子弹
    bullet2 = []
    bullet2_index=0
    BULLET2_NUM = 8
    for i in range(BULLET2_NUM //2):
        bullet2.append(bullet.Bullet2((me.rect.centerx-33,me.rect.centery)))
        bullet2.append(bullet.Bullet2((me.rect.centerx+30,me.rect.centery)))
    draw=[]
    draw_line=[]
    draw_enemy=[]
    #延时
    delay = 100

    pause=False
    is_bullet = False
    switch = False
    runing=True
    while runing:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        if not (delay%5):
            switch = not switch
        
        if not pause:
            draw=[]
            #画子弹_发射子弹
            if not (delay%10):
                bullet_sound.play()
                if is_bullet:
                    bullets=bullet2
                    bullets[bullet2_index].reset((me.rect.centerx-33,me.rect.centery))
                    bullets[bullet2_index+1].reset((me.rect.centerx+30,me.rect.centery))
                    bullet2_index = (bullet2_index+2) % BULLET2_NUM
                    
 
                else:
                    bullets=bullet1
                    bullets[bullet1_index].reset(me.rect.midtop)
                    bullet1_index = (bullet1_index+1) % BULLET1_NUM
                    
            #画子弹_子弹飞行
            for b in bullets:
                if b.active:
                    b.move()
                    draw.append((b.image,b.rect))

            #画自己的飞机
            if me.active:
                if switch:
                    draw.append((me.image1,me.rect))
                else:
                    draw.append((me.image2,me.rect))

            
        screen.blit(background,(0,0))
        for i,j in draw:
            screen.blit(i,j)
        delay -=1
        if not delay:
            delay = 100

        pygame.display.flip()
        clock.tick(60)


if __name__=='__main__':
    try:
        main()
    except SystemExit:
        pass
    except:
        traceback.print_exc()
        pygame.quit()
        input()
